﻿using System.Collections.Generic;
using OpusSuite.Models.Model.Shared;
using OpusSuite.Models.Model.Ref;
using OpusSuite.Utility;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace OpusSuite.Models.Model.WebAdmin
{
    [Table("userprofile")]
    public class UserProfile : ModelBase
    {
        public int UserProfileId { get; set; }

        [Required]
        [StringLength(256, MinimumLength = 4)]
        public string DisplayName { get; set; }

        public virtual Country Country { get; set; }

        public virtual IList<UserProfileRole> Roles { get; set; }

        public virtual Theme Theme { get; set; }

        public virtual IList<UserProfileSetting> Settings { get; set; }

        public UserProfileSetting AddUserSetting(Enums.SettingType type, string value)
        {
            UserProfileSetting s = new UserProfileSetting { Type = type, Value = value };

            if (Settings == null)
                Settings = new List<UserProfileSetting>();

            Settings.Add(s);

            return s;
        }

        public UserProfileSetting AddUserSetting(Enums.SettingType type, bool value)
        {
            return AddUserSetting(type, (value ? "True" : "False"));
        }

        public bool UserSetting(Enums.SettingType type, bool defaultValue=false)
        {
            UserProfileSetting s = Settings.FirstOrDefault(x => x.Type == type);
            if (s != null)
            {
                return s.Value != null && s.Value == "True";
            }

            return defaultValue;
        }

        public string UserSetting(Enums.SettingType type, string defaultValue="")
        {
            UserProfileSetting s = Settings.FirstOrDefault(x => x.Type == type);
            if (s != null)
            {
                return s.Value;
            }

            return defaultValue;
        }
    }
}
